﻿//Morgan Hansen
//
//last update 4/15/2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using MySql.Data.MySqlClient;
using System.Threading;

namespace Controller
{
    class BlackBox
    {
        private int boxID;  // need to change in the design Doc
        private Command command;
        private NetworkStream clientStream;
        private bool currentState = false; // need to Change Design Doc

        public BlackBox(int id, NetworkStream stream)
        {
            boxID = id;
            command = new Command(null, null, 0);
            clientStream = stream;
        }
        public void SendCommand(bool waterOn_Off)//stubbed
        {
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer;
            byte[] blackboxRe = new byte[7];

            if (waterOn_Off == true)
            {
                buffer = encoder.GetBytes("WWW"); //Water on
                currentState = true;
            }
            else
            {
                buffer = encoder.GetBytes("www"); //Water off
                currentState = false;
            }
            clientStream.Write(buffer, 0, buffer.Length); //send command
            Thread.Sleep(100);
            clientStream.Flush();
            // clientStream.Read(blackboxRe, 0, 7);// get reply
            //if (Convert.ToBoolean(blackboxRe[6]) != currentState)
            //{
            //    currentState = Convert.ToBoolean(blackboxRe[6]);
            //    return (true);
            //}
            //else
            //{
            //    return (false);
            //}
        }

        public bool MonitorCommands()
        {
            bool dbStatus;
            string connectionString = "datasource=localhost;username=tara;password=tarapass;database=vino";
            MySqlConnection connection = new MySqlConnection(connectionString);
            MySqlCommand sqlcommand = connection.CreateCommand();
            MySqlDataReader Reader;
            sqlcommand.CommandText = "SELECT CommandTypeID, CommandFlag FROM tblcommand WHERE BlackBoxID = " + boxID.ToString();
            connection.Open();
            Reader = sqlcommand.ExecuteReader();
            if (Reader.HasRows)
            {
                Dictionary<string, string> row = new Dictionary<string, string>();
                while (Reader.Read())
                {

                    for (int i = 0; i < Reader.FieldCount; i++)
                        row.Add(Reader.GetName(i), Reader.GetValue(i).ToString());

                }
                connection.Close();
                dbStatus = bool.Parse(row["CommandFlag"]);
                if (dbStatus != currentState)
                {

                    command.SetCommandFlag(dbStatus);

                    /*return*/
                    SendCommand(command.GetCommandFlag());// change to command.commandflag
                    return true;
                }
                else return false;
            }//if has rows
            connection.Close();
            return false;
        }//MonitorCommands

        public bool InitBox()
        {
            //taken care of in Notication::CreateStreams
            return (true);
        }//InitBox

        public Command GetCommand()
        {
            return (command);
        }//GetCommand

        public int GetBoxID()
        {
            return (boxID);
        }//getBoxID
    }//class
}//namespace
